package com.hutao.sso.controller.hospital;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hutao.sso.pojo.newAuthority.UserHospitals;
import com.hutao.sso.pojo.user.Users;
import com.hutao.sso.service.baseInfo_remote.BasicDataService;
import com.hutao.sso.service.hospital.UserHospitalService;
import com.hutao.sso.util.ChacheManager;
import com.hutao.sso.util.Constant;
import com.hutao.sso.util.MD5;
import com.hutao.sso.util.Page;
import com.hutao.sso.util.Result;

@RequestMapping("/userHospital")
@Controller
public class UserHospitalController {

	@Autowired
	private UserHospitalService userHospitalService;
	
	@Autowired
	private BasicDataService basicDataService;
	
	@Value("${config.resource.core.url}")
	private String configResourceCoreUrl;


	@PostMapping("/findUserHospitalListPage")
	@ResponseBody
	public Map<String, Object> findUserHospitalListPage(@RequestParam(value= "page", defaultValue = "1") int currentPage,
			@RequestParam(value = "loginName", required =false) String loginName, String name, String officeCode) {
		Map<String, Object> params = new HashMap<>();
		Map<String, Object> results = new HashMap<>();
		
		Page<UserHospitals> page= new Page<>();
		page.setCurrentPage(currentPage);
		page.setPageSize(10);
		params.put("page", page);
		params.put("loginName", loginName);
		params.put("name", name);
		params.put("officeCode", officeCode);
		
		List<UserHospitals> userHospitalList = userHospitalService.findUserHospitalsListPage(params);
		page.setList(userHospitalList);
		page.setRows((int)userHospitalService.findUserHospitalsCount(params));
		
		results.put("page", page);
		return results;
	}
	
	@PostMapping("/saveUserHospitals")
	@ResponseBody
	public Result saveUserHospitals(UserHospitals userHospitals, HttpSession session) {
		try {
			
			Users user = (Users)session.getAttribute("current_user");
			if(null != user) {
				userHospitals.setMedicalMainstayId(user.getMedicalMainstayId());
			}
			
			Map<String, Object> params = new HashMap<>();
			params.put("loginName", userHospitals.getLoginName());
			params.put("medicalMainstayId", user!=null?user.getMedicalMainstayId():null);
			
			String officeCode = basicDataService.getOfficeCode(userHospitals.getOfficeName(), configResourceCoreUrl);
			userHospitals.setOfficeCode(officeCode);
			
			userHospitals.setPassword(MD5.md5(userHospitals.getPassword()));
			
			if(userHospitalService.findUserHospitalsByLoginNameAndMedicalMainstayId(params) > 0) {
				return new Result(false, "该账号名称已经存在!");
			}
			
			if(userHospitalService.saveUserHospitals(userHospitals) > 0) {
				return new Result(true, "添加成功!");
			}
			return new Result(false, "添加失败!");
			
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		
	}
	
	@PostMapping("/updateUserHospitalByIsAcvicateAndId")
	@ResponseBody
	public Result updateUserHospitalByIsAcvicateAndId(String id, String isActivate, HttpSession session) {
		
		try {
			Map<String, Object> params = new HashMap<>();
			params.put("isAcvicate", isActivate);
			params.put("id", id);
			Users user = (Users)session.getAttribute("current_user");
			params.put("medicalMainstayId", user.getMedicalMainstayId());
			if(userHospitalService.updateUserHospitalByIsAcvicateAndId(params) > 0) {
				return new Result(true, "修改成功!");
			}
			return new Result(false, "修改失败!");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
			
		}
	}
	
	@PostMapping("/updatePasswordById")
	@ResponseBody
	public Result updatePasswordById(String id, String password, HttpSession session) {
		
		try {
			Map<String, Object> params = new HashMap<>();
			params.put("password", MD5.md5(password));
			params.put("id", id);
			Users user = (Users)session.getAttribute("current_user");
			params.put("medicalMainstayId", user.getMedicalMainstayId());
			if(userHospitalService.updatePasswordById(params) > 0) {
				return new Result(true, "修改成功!");
			}
			return new Result(false, "修改失败!");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
			
		}
	}
	
	@PostMapping("/updateUserName")
	@ResponseBody
	public Result updateUserNameAndOfficeCode(String id, String userName, String officeName, HttpSession session) {
		try {
			Map<String, Object> params = new HashMap<>();
			params.put("id", id);
			params.put("userName", userName);
			params.put("officeName", officeName);
			Users user = (Users)session.getAttribute("current_user");
			params.put("medicalMainstayId", user.getMedicalMainstayId());
			String officeCode = basicDataService.getOfficeCode(officeName, configResourceCoreUrl);
			params.put("officeCode", officeCode);
			
			if (userHospitalService.updateUserNameByAndOfficeIdId(params) > 0) {
				return new Result(true, "修改成功!");
			}
			
			return new Result(false, "修改失败!");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "修改失败!");
		}
	}
	
	
	


}




